package md.frolov.bible.initializers;

public class MyStringBuilder {
	char[] chars;
	int size = 0;
	
	public MyStringBuilder(int capacity) {
		chars = new char[capacity];
	}

	public MyStringBuilder() {
		this(32);
	}

	private void expand(){
		char[] oldchars = chars;
		chars = new char[chars.length*2];
		for(int i=0;i<oldchars.length;i++){
			chars[i] = oldchars[i];
		}
	}
	
	public MyStringBuilder appendCodePoint(int cp){
		size += Character.toChars(cp, chars, size);
		if(size>=chars.length - 4) expand();
		return this;
	}
	
	public void clear(){
		size = 0;
	}
	
	public String toString(){
//		if(size == 0) return "";
		char[] res = new char[size];
		for(int i=0;i<size;i++)
			res[i] = chars[i];
		return new String(res);
	}
	
	public int length(){
		return size;
	}	
}
